package pl.iachoo.ctl.tabory.dao.hibernate.services;

import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;

import pl.iachoo.ctl.tabory.dao.hibernate.interfaces.ILogonService;
import pl.iachoo.ctl.tabory.dao.hibernate.vo.LogonVO;
import pl.iachoo.ctl.tabory.dao.hibernate.vo.UserVO;

public class LogonServiceImpl extends BaseDaoService<LogonVO> implements
		ILogonService {

	private final static Logger log = Logger.getLogger(LogonServiceImpl.class);

	private static LogonServiceImpl instance;

	private LogonServiceImpl() {

	}

	public static LogonServiceImpl getInstance() {
		if (instance == null) {
			log.info("Creating new instance of service: LogonServiceImpl");
			instance = new LogonServiceImpl();
		}
		log.info("Retriving service: LogonServiceImpl.");
		return instance;
	}

	@Override
	public LogonVO addLogon(UserVO user) throws Exception {
		log.debug("Adding new logon info for user: " + user);
		LogonVO logon = new LogonVO();
		logon.setLogonDts(new Date());
		logon.setUserVO(user);
		saveOrUpdate(logon);
		return logon;
	}

	@Override
	public List<LogonVO> getLogons(UserVO user) throws Exception {
		log.debug("Getting logon info for user: " + user);
		LogonVO criterion = new LogonVO();
		criterion.setUserVO(user);
		return loadList(LogonVO.class, criterion);
	}

}
